package com.lwj.liwanjia.view.activity.personal;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.FankuiBean;
import com.lwj.liwanjia.impl.FankuiView;
import com.lwj.liwanjia.presenter.FankuiPresenter;
import com.lwj.liwanjia.utils.SpfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by 001 on 2017/9/9.
 */

public class FanKuiActivity extends BaseActivity implements FankuiView {


    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ed_fankui)
    EditText edFankui;
    @BindView(R.id.tv_number)
    TextView tvNumber;

    private String token;
    private String text;
    private FankuiPresenter presenter;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("反馈意见");
        presenter = new FankuiPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();
        edFankui.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (s.length() == 200) {
                    Toast.makeText(FanKuiActivity.this, "字数不能超过200字", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvNumber.setText("" + s.length());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fan_kui;
    }

    @OnClick({R.id.tv_left, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_submit:
                text = edFankui.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                presenter.subFankui(token, text);
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "反馈失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void subFankui(FankuiBean fankuiBean) {
        if (fankuiBean.getData() == 1) {
            Toast.makeText(this, "反馈成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
    }
}
